package com.imti.service.impl;

import com.imti.mapper.PaperMapper;
import com.imti.model.*;
import com.imti.model.dto.PaperTitle;
import com.imti.service.PaperService;
import com.imti.util.PageBean;
import com.imti.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    // 注入RedisTemplate
    private final RedisTemplate<String, Object> redisTemplate;

    public PaperServiceImpl(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public PageBean findAll(PageBean page, Paper paper) {
        if(page.getCurrtPage()==null){
            page.setCurrtPage(1);
        }
        if(page.getPageLimit()==null){
            page.setPageLimit(5);
        }
        int totalCount=paperMapper.getPaperCount(paper);
        Map map=new HashMap();
        map.put("paper",paper);
        map.put("page",page);
        List<Paper> list = paperMapper.findAll(map);
        PageBean pages=new PageBean(totalCount,page.getCurrtPage(),page.getPageLimit(),list,"papers",getParams(paper));
        return pages;
    }
    /**
     * 封装分页搜索查询的参数
     * @param
     * @return
     */
    public String getParams(Paper paper){
        StringBuilder sb=new StringBuilder();
        if(paper.getTitle()!=null && !"".equals(paper.getTitle())){
            sb.append("&title=").append(paper.getTitle());
        }
        return sb.toString();
    }

    @Override
    public int addPaper(Paper paper) {
        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        paper.setUserId(userId);
        return paperMapper.addPaper(paper);
    }

    @Override
    public List<Repo> selectRepo() {
        return paperMapper.showRepo();
    }

    @Override
    public int saveRepo(Integer pid,Integer repo) {
        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        return paperMapper.saveRepo(pid,repo,userId);
    }

    @Override
    public int selectPaperId(String title) {
        return paperMapper.selectPaperId(title);
    }

    @Override
    public void selectPaperById(Integer id) {
        //查询试卷规则信息
        Paper paper = paperMapper.selectPaperById(id);
        //获取题目表总数量
        int max = paperMapper.selectAllQuestion();
        List<Integer> randomNums = RandomUtils.generateRandomIntegers(max,1,max);
        int m = 0;
        int n = 0;
        int z = 0;
        if (paper.getRadioCount()!=null){
            for (Integer num : randomNums) {
                //查出单选题
                Integer radioTitle = paperMapper.findTitleById(id, num, 1);
                if (radioTitle!=null){
                    paperMapper.setTitle(radioTitle, id);
                    m++;
                }
                if (m==paper.getRadioCount().intValue()){
                    break;
                }
            }
        }
        if (paper.getCheckCount()!=null){
            for (Integer num : randomNums) {
//            System.out.println(num);
                //查出多选题
                Integer checkTitle = paperMapper.findTitleById(id, num, 2);
                if (checkTitle!=null){
                    paperMapper.setTitle(checkTitle, id);
                    n++;
                }
                if (n==paper.getCheckCount().intValue()){
                    break;
                }
            }
        }
        if (paper.getJudgeCount()!=null){
            for (Integer num : randomNums) {
                //查出判断题
                Integer judgeTitle =  paperMapper.findTitleById(id,num,3);
                if (judgeTitle!=null){
                    paperMapper.setTitle(judgeTitle, id);
                    z++;
                }
                if (z==paper.getJudgeCount().intValue()){
                    break;
                }
            }
        }

    }

    @Override
    public List<PaperTitle> selectPaperTitleById(Integer id,Integer type) {
        return paperMapper.selectPaperTitleById(id,type);
    }

    @Override
    public int deleteRepo(Integer id) {
        return paperMapper.deleteRepoById(id);
    }

    @Override
    public void showPaper(Integer id,HttpServletRequest request) {
        int thispaper = paperMapper.isHavaPaper(id);
        if (thispaper==0){
            selectPaperById(id);//生成试卷
        }
        //单选题列表
        List<PaperTitle> radioList = paperMapper.selectPaperTitleById(id,1);
        //多选题列表
        List<PaperTitle> checkList = paperMapper.selectPaperTitleById(id,2);
        //判断题列表
        List<PaperTitle> judgeList = paperMapper.selectPaperTitleById(id,3);
        //查询试卷的答案
//        for (PaperTitle p : radioList) {
//            List<Answer> answerList = selectAnswerByQid(p.getAnswerId());
//            p.setAnswerList(answerList);
//        }
//        for (PaperTitle p : checkList) {
//            List<Answer> answerList = selectAnswerByQid(p.getAnswerId());
//            p.setAnswerList(answerList);
//        }
//        for (PaperTitle p : judgeList) {
//            List<Answer> answerList = selectAnswerByQid(p.getAnswerId());
//            p.setAnswerList(answerList);
//        }
//        System.out.println(radioList);
        request.setAttribute("radioList",radioList);
        request.setAttribute("checkList",checkList);
        request.setAttribute("judgeList",judgeList);
    }

    private List<Answer> selectAnswerByQid(Integer id) {
        return paperMapper.selectAnswerByQid(id);
    }
}
